package vn.conghuy.talkwithme.responsor.responsor;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import vn.conghuy.talkwithme.MyApp;
import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.utils.IntWrapper;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;

public class FacebookResponsor extends Responsor {
	
	private static final int RELATIVE_ASK_STATUS = 1;
	
	MyApp myApp;
	public FacebookResponsor(Context context) {
		super(context);
		myApp = (MyApp)context.getApplicationContext();
	}
	
	@Override
	public String response(String request) {

		if(Utils.isExistStringInArray(SqlData.stopWords, request)){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.taskStop);
		}
		
		switch (relativeLastResponse) {
		case RELATIVE_NONE:
			StringWrapper facebookPostVerbWrapper = new StringWrapper();
			StringWrapper facebookPostKeywordWrapper = new StringWrapper();
			
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.facebookPostVerbs, request, facebookPostVerbWrapper)
					&& Utils.isExistElemInArrayIsWordOfStr(SqlData.facebookPostKeywords, request, facebookPostKeywordWrapper)){
				
				String checkLoginStr = checkFacebookLoginAndResponse();
				if(checkLoginStr != null) return checkLoginStr;
					
				relativeLastResponse = RELATIVE_ASK_STATUS;
				return Utils.getRandomString(SqlData.facebookPostAskStatus);
				
			}
			else{
				if(Utils.isStringMatchRegexInArray(request, SqlData.facebookCheckNotificationsRegexes)){
					
					String checkLoginStr = checkFacebookLoginAndResponse();
					if(checkLoginStr != null) return checkLoginStr;
					
					IntWrapper numNotiWrapper = new IntWrapper();
					ArrayList<String> notifications = myApp.facebook.notifications(numNotiWrapper);
					int numberNoti = numNotiWrapper.getInt();
					StringBuilder strBuilder = new StringBuilder();
					strBuilder.append(context.getString(R.string.you_have)).append(" ").append(numberNoti)
								.append(" ").append(context.getString(R.string.notifications));
					if(numberNoti > 0){
						if(numberNoti > 1) strBuilder.append("s");
						strBuilder.append(" :\n\n");
					}
					
					for(int i = 0; i < notifications.size(); i++){
						strBuilder.append((i+1)).append(". ").append(notifications.get(i)).append("\n");
					}
					relativeLastResponse = RELATIVE_NONE;
					return strBuilder.toString();
				}
			}
			break;
			
		case RELATIVE_ASK_STATUS:
			String checkLoginStr = checkFacebookLoginAndResponse();
			if(checkLoginStr != null) return checkLoginStr;
			
			boolean isSuccess = myApp.facebook.updateStatus(request);
			relativeLastResponse = RELATIVE_NONE;
			if(isSuccess == true){
				return Utils.getRandomString(SqlData.facebookPostSuccess);
			}
			return Utils.getRandomString(SqlData.facebookPostFail);
		}
		return super.response(request);
	}
	
	String checkFacebookLoginAndResponse(){
		if(myApp.facebook.isLogin() == false){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.facebookRequireLogin);
		}
		return null;
	}
}
